package cn.windy.samary.utils;

import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 字符串操作工具类
 */
public class StringUtil {
    /**
     * 帮助输入框中所输入的字符串首字母
     *
     * @param query
     * @return
     */
    public static String getFirstCharForQueryString(String query)
            throws Exception {
        String firstChar = "";
        if (query.length() >= 1) {
            firstChar = query.substring(0, 1);
            return firstChar;
        } else {
            throw new Exception("输入条件必须至少有一个字符");
        }
    }

    /**
     * 生成一个含有数字和字母的随机数
     *
     * @param show 随机数位数
     * @return
     */
    public static String getRandomNumber(int show) {
        Random random;
        BufferedImage image;
        Font font;
        int distance = 15;
        String str = "Ab1Cd7EfG5hJk8Lm6NPqRSTuVwXYz0123456789";
        random = new Random();
        image = new BufferedImage(70, 25, BufferedImage.TYPE_3BYTE_BGR);
        font = new Font("Arial", Font.PLAIN, 20);
        Graphics d = image.getGraphics();
        d.setColor(Color.WHITE);
        d.fillRect(0, 0, image.getWidth(), image.getHeight());
        d.setColor(new Color(random.nextInt(100) + 100,
                random.nextInt(100) + 100, random.nextInt(100) + 100));
        for (int i = 0; i < 10; i++) {
            d.drawLine(random.nextInt(image.getWidth()),
                    random.nextInt(image.getHeight()),
                    random.nextInt(image.getWidth()),
                    random.nextInt(image.getHeight()));
        }
        d.setColor(Color.BLACK);
        d.setFont(font);
        StringBuilder checkCode = new StringBuilder();
        char tmp = 0;
        int x = -distance;
        for (int i = 0; i < show; i++) {
            tmp = str.charAt(random.nextInt(str.length() - 1));
            checkCode.append(tmp);
            x = x + distance;
            d.setColor(new Color(random.nextInt(100) + 50,
                    random.nextInt(100) + 50, random.nextInt(100) + 50));
            d.drawString(tmp + "", x,
                    random.nextInt(image.getHeight() - (font.getSize()))
                            + (font.getSize()));
        }
        return checkCode.toString();
    }

    /**
     * 判断两个以逗号隔开的字符串中的内容是否相同
     *
     * @param source
     * @param target
     * @return
     */
//	public static boolean judgeSetString(String source, String target) {
//		if (StringUtils.hasText(source) && StringUtils.hasText(target)) {
//			if (source.indexOf(",") != -1 && target.indexOf(",") != -1) {
//				Set<Long> sourceSet = new HashSet<Long>();
//				Set<Long> targetSet = new HashSet<Long>();
//				inSet(sourceSet, source);
//				inSet(targetSet, target);
//				if (sourceSet.equals(target)) {
//					return true;
//				} else {
//					return false;
//				}
//			} else if (source.indexOf(",") == -1 && target.indexOf(",") == -1) {
//				if (source.equals(target)) {
//					return true;
//				} else {
//					return false;
//				}
//			} else {
//				return false;
//			}
//		} else {
//			return false;
//		}
//	}
    private static void inSet(Set<Long> set, String str) {
        String[] strarr = str.split(",");
        for (String a : strarr) {
            set.add(Long.parseLong(a));
        }
    }

    /**
     * 根据spring的类名获取容器对象名称
     *
     * @param simpleClassName
     * @return
     */
    public static String getSpringBeanName(String simpleClassName) {
        if (StringUtils.hasText(simpleClassName)) {
            return simpleClassName.substring(0, 1).toLowerCase()
                    + simpleClassName.substring(1, simpleClassName.length());
        }
        return simpleClassName;
    }

    /**
     * 替换字符串的转义字符
     *
     * @param str
     * @return
     */
    public static String replaceBlank(String str) {
        String dest = "";
        if (str != null) {
            Pattern p = Pattern.compile("\\s*|\t|\r|\n");
            Matcher m = p.matcher(str);
            dest = m.replaceAll("");
        }
        return dest;
    }

    /**
     * 替换字符串的转义字符
     *
     * @param str
     * @return
     */
    public static String replaceString(String str) {
        String dest = "";
        if (str != null) {
            Pattern p = Pattern.compile("\\t|\r|\n");
            Matcher m = p.matcher(str);
            dest = m.replaceAll("");
        }
        return dest;
    }

    public static String queryGetMethodString(String field) {
        StringBuilder result = new StringBuilder();
        if (!ObjectUtils.isEmpty(field) && field.length() > 1) {
            result.append("get");
            result.append(field.substring(0, 1).toUpperCase());
            result.append(field.substring(1, field.length()));
        }
        return result.toString();
    }

    public static String querySetMethodString(String field) {
        StringBuilder result = new StringBuilder();
        if (!ObjectUtils.isEmpty(field) && field.length() > 1) {
            result.append("set");
            result.append(field.substring(0, 1).toUpperCase());
            result.append(field.substring(1, field.length()));
        }
        return result.toString();
    }
}